Skip to content

Conversation

@skirtles-code
Copy link
Contributor

@skirtles-code skirtles-code commented Nov 17, 2024

This PR aims to address several related edge cases.

Case 1

The original motivation came from vuejs/pinia#2812.

Long story short, consider the following code:

const c = computed(() => console.log('here'))
const r = reactive({ c })
toRef(r, 'c')

The call to toRef currently triggers the logging, even if the returned ref is never used:

While attempting to fix this, I kept running into other edge cases, mostly involving toRef and shallowReactive/shallowReadonly. I've tried to fix all of those edge cases too.

Case 2

Consider the following:

const s = shallowReactive({ num: ref(0) })
const t = toRef(s, 'num')
s.num = ref(1)

This should result in t.value being 1, but currently it ends up as 0.

Case 3

Consider this example:

const s = shallowReactive({ num: ref() })
const t = toRef(s, 'num', 7)

t.value should be 7, respecting the default value. But currently it is undefined.

Case 4

This is somewhat separate, but it impacted the tests I wrote for array handling with toRef.

Consider the following:

const arr = reactive([])
const r = ref(0)
arr.foo = r
arr.foo = 2

Arrays don't unwrap refs used as elements, but they do unwrap arrays added using custom properties such as foo. Essentially, custom properties on reactive arrays behave just like properties of normal reactive objects. Apart from in the case outlined above, where the ref is replaced rather than updated by arr.foo = 2. I've changed this in baseHandlers.ts, so that updating a custom property on an array behaves the same as for an object:

Notes on the changes

The key change is to move the logic for handling nested refs from propertyToRef to inside ObjectRefImpl.

Detecting whether a source will automatically unwrap refs is a bit tricky. It isn't sufficient just to check for isShallow, as you could have shallowReadonly wrapped around reactive. Instead I've recursively checked each wrapper proxy to check whether any level would unwrap.

As noted earlier, arrays also pose a specific challenge, as they don't unwrap elements even inside reactive or readonly.

There is one existing test case that no longer passes. Specifically, this one:

const r = { x: ref(1) }
expect(toRef(r, 'x')).toBe(r.x)

This is expecting the exact same ref to be returned by toRef. But returning the same ref is the underlying cause of most of the edge cases outlined above. From what I can tell, the original motivation for that test case was just to ensure that nested refs are unwrapped, not that they need to be ===. Returning an equivalent ref should be sufficient.

I do wonder whether there's an easier way to implement all of this, but currently I'm not seeing it.

Summary by CodeRabbit

Release Notes

  • New Features

    • Exported shallowReadonly API for creating shallow readonly reactive proxies.
  • Improvements

    • Enhanced ref behavior when used with non-reactive objects, reactive objects, and arrays.
    • Improved handling of nested references within reactive proxies.
    • Optimized proxy behavior for numeric array indices.
  • Tests

    • Added comprehensive test coverage for ref interactions across all reactive wrapper contexts, including edge cases with nested references and array operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@github-actions
Copy link

github-actions bot commented Nov 17, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 103 kB (+223 B) 39 kB (+77 B) 35.1 kB (+108 B)
vue.global.prod.js 161 kB (+223 B) 58.9 kB (+88 B) 52.4 kB (+116 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.8 kB (+1 B) 18.3 kB (+4 B) 16.8 kB (+2 B)
createApp 55 kB (+1 B) 21.4 kB (+5 B) 19.6 kB (+5 B)
createSSRApp 59.3 kB (+1 B) 23.1 kB (+5 B) 21.1 kB (-10 B)
defineCustomElement 60.6 kB (+1 B) 23.1 kB (+4 B) 21.1 kB (+10 B)
overall 69.3 kB (+1 B) 26.6 kB (+4 B) 24.3 kB (+22 B)

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 17, 2024

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@12420

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@12420

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@12420

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@12420

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@12420

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@12420

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@12420

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@12420

@vue/shared

npm i https://pkg.pr.new/@vue/shared@12420

vue

npm i https://pkg.pr.new/vue@12420

@vue/compat

npm i https://pkg.pr.new/@vue/compat@12420

commit: d9cdf22

@edison1105
Copy link
Member

edison1105 commented Nov 18, 2024

This PR has some performance impact on ref. The baseline is the main branch.
 ✓ packages/reactivity/__benchmarks__/ref.bench.ts (4) 11856ms
   ✓ ref (4) 11854ms
     name                       hz     min     max    mean     p75     p99    p995    p999     rme   samples
   · create ref      11,642,281.81  0.0000  0.2558  0.0001  0.0001  0.0001  0.0001  0.0002  ±0.21%   5821141  [0.55x] ⇓
     create ref      21,231,067.58  0.0000  0.1998  0.0000  0.0000  0.0001  0.0001  0.0001  ±0.21%  10615534  (baseline)
   · write ref       10,862,161.63  0.0000  2.2381  0.0001  0.0001  0.0001  0.0001  0.0002  ±0.95%   5431081  [0.53x] ⇓
     write ref       20,480,924.69  0.0000  0.5687  0.0000  0.0000  0.0001  0.0001  0.0002  ±0.55%  10240463  (baseline)
   · read ref        15,235,715.02  0.0000  0.7686  0.0001  0.0001  0.0001  0.0002  0.0002  ±0.59%   7617858  [0.61x] ⇓
     read ref        24,909,611.96  0.0000  0.7011  0.0000  0.0000  0.0000  0.0001  0.0001  ±0.76%  12454807  (baseline)
   · write/read ref  10,840,440.94  0.0000  0.3820  0.0001  0.0001  0.0001  0.0001  0.0002  ±0.39%   5420221  [0.53x] ⇓
     write/read ref  20,325,089.63  0.0000  0.6985  0.0000  0.0000  0.0001  0.0001  0.0002  ±0.71%  10162545  (baseline)

with the following steps:

1. switch to main branch
2. nr prebench-compare
3. nr bench ref
4. switch to this PR
5. nr prebench-compare
6. nr bench-compare ref

envinfo:

  System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M1
    Memory: 127.44 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.7.0 - /usr/local/bin/node

@edison1105 edison1105 added scope: reactivity need discussion 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. labels Nov 18, 2024
@skirtles-code
Copy link
Contributor Author

@edison1105 Would you be able to try that performance benchmark again?

I tried running it locally, but it seems to get stuck running bench-compare. It starts off OK, then it just gets stuck at 100% CPU and never completes.

I don't think my changes should impact those benchmarks. The benchmark you mentioned seems to be testing ref(), which should be unchanged by this PR.

I would expect some performance impact on toRef() when passing toRef(obj, 'property'), but that doesn't seem to be what's tested in the benchmarks.

@edison1105
Copy link
Member

@skirtles-code

I tested it again. This time, I repeated step 6 above three times, and the results showed that performance was not affected. Below are the results from the third execution.

The above test results were obtained from the first time run nr bench-compare ref. It is strange that there is a significant performance difference.

✓ packages/reactivity/__benchmarks__/ref.bench.ts (4) 16730ms
   ✓ ref (4) 16728ms
     name                       hz     min     max    mean     p75     p99    p995    p999     rme   samples
   · create ref      21,076,099.66  0.0000  0.2064  0.0000  0.0000  0.0001  0.0001  0.0002  ±0.23%  10538050  [1.00x] ⇓
     create ref      21,152,011.37  0.0000  0.5231  0.0000  0.0000  0.0001  0.0001  0.0002  ±0.38%  10576006  (baseline)
   · write ref       20,557,804.48  0.0000  0.4285  0.0000  0.0000  0.0001  0.0001  0.0001  ±0.51%  10278903  [1.02x] ⇑
     write ref       20,076,703.63  0.0000  0.5026  0.0000  0.0000  0.0001  0.0001  0.0002  ±0.52%  10038353  (baseline)
   · read ref        24,080,925.98  0.0000  0.7341  0.0000  0.0000  0.0000  0.0001  0.0002  ±1.10%  12040464  [0.99x] ⇓
     read ref        24,253,846.74  0.0000  1.1237  0.0000  0.0000  0.0000  0.0001  0.0001  ±1.02%  12126924  (baseline)
   · write/read ref  19,876,380.25  0.0000  0.5281  0.0001  0.0000  0.0001  0.0001  0.0002  ±0.64%   9938191  [1.03x] ⇑
     write/read ref  19,240,027.69  0.0000  2.3183  0.0001  0.0000  0.0001  0.0001  0.0002  ±1.21%   9620095  (baseline)

@edison1105 edison1105 added ready to merge The PR is ready to be merged. and removed need discussion labels Nov 20, 2024
@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 2025

Warning

Rate limit exceeded

@edison1105 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 15 minutes and 18 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 4455ffe and d9cdf22.

📒 Files selected for processing (1)
  • packages/reactivity/__tests__/ref.spec.ts (3 hunks)

Walkthrough

The changes extend Vue's reactivity system with enhanced toRef behavior for nested refs and shallow contexts. Adds comprehensive test coverage for toRef edge cases including array handling, nested ref propagation, and shallow variant interactions. Exports shallowReadonly as public API and refines ref unwrapping logic in baseHandlers and ref implementation to correctly handle array integer-key operations and nested ref scenarios.

Changes

Cohort / File(s) Summary
Test Coverage Expansion
packages/reactivity/__tests__/ref.spec.ts
Adds extensive tests for toRef behavior with non-reactive objects, lazy evaluation across wrapper contexts (plain, reactive, readonly, shallowReactive, shallowReadonly), array handling with refs and symbolic keys, nested ref proxy behavior, and warning messages for disallowed operations.
API Exports
packages/reactivity/src/reactive.ts
Adds public export of shallowReadonly function.
Ref Unwrapping Logic
packages/reactivity/src/baseHandlers.ts
Introduces isArrayWithIntegerKey helper variable; refines conditional path for unwrapping nested refs to exclude array integer-key cases; adjusts hadKey check to use shared variable instead of repeated explicit checks.
ObjectRef Implementation
packages/reactivity/src/ref.ts
Introduces shallow-unwrapping logic with _raw and _shallow internal fields in ObjectRefImpl; updates getter to conditionally unwrap values based on shallow flag; modifies setter to route nested refs directly when shallow; simplifies propertyToRef to always construct new ObjectRefImpl rather than returning existing refs; updates dep lookup to use cached _raw.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant toRef
    participant ObjectRefImpl
    participant Proxy
    participant Source

    User->>toRef: toRef(source, key)
    activate toRef
    toRef->>ObjectRefImpl: new ObjectRefImpl(source, key, defaultValue)
    activate ObjectRefImpl
    ObjectRefImpl->>Source: Determine if shallow/raw
    ObjectRefImpl-->>ObjectRefImpl: Store _raw, _shallow
    deactivate ObjectRefImpl
    toRef-->>User: Return Ref
    deactivate toRef

    User->>Ref: ref.value (get)
    activate Ref
    rect rgb(200, 230, 255)
        note over Ref: Shallow unwrapping logic
        alt _shallow flag is true
            Ref->>Source: Get source[key]
            Ref->>Ref: Unwrap nested proxy if present
            Ref-->>User: Return unwrapped value
        else _shallow flag is false
            Ref->>Source: Get source[key]
            Ref-->>User: Return raw value
        end
    end
    deactivate Ref
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • baseHandlers.ts: Logic changes for array integer-key handling and ref unwrapping paths require careful verification of affected edge cases across multiple scenarios
  • ref.ts: ObjectRefImpl modifications introduce shallow-unwrapping complexity and change propertyToRef behavior; requires validation that nested ref linkage works correctly in all contexts
  • Test file scale: Large test additions necessitate review of correctness across multiple test cases and wrapper combinations
  • Interaction risk: Changes across multiple files interact in ways affecting core reactivity behavior, particularly for nested refs and array operations

Possibly related PRs

Suggested labels

🔧 refactor, ✅ tests, 📦 reactivity

Suggested reviewers

  • jh-leong

Poem

🐰 A rabbit hops through nested refs so deep,
Where shallow unwrapping helps values leap,
Array keys align, and proxies align,
shallowReadonly now shines in the design! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(reactivity): toRef edge cases for ref unwrapping' is specific and clearly summarizes the main changes—addressing edge cases in toRef's ref unwrapping behavior across multiple scenarios.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@edison1105
Copy link
Member

/ecosystem-ci run

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 247b2c2 and 4455ffe.

📒 Files selected for processing (3)
  • packages/reactivity/__tests__/ref.spec.ts (3 hunks)
  • packages/reactivity/src/baseHandlers.ts (2 hunks)
  • packages/reactivity/src/ref.ts (3 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: naramdash
Repo: vuejs/core PR: 14058
File: packages/reactivity/src/collectionHandlers.ts:252-271
Timestamp: 2025-11-05T14:53:01.052Z
Learning: In Vue's reactivity system, collection methods that create new collections (like Array's slice(), concat(), filter() and Set's difference(), intersection(), union(), symmetricDifference()) intentionally return raw (non-reactive) collections, not reactive proxies. This is by design - only the values/elements inside can remain reactive if they were originally reactive.
🧬 Code graph analysis (3)
packages/reactivity/__tests__/ref.spec.ts (2)
packages/reactivity/src/ref.ts (4)
  • ref (61-63)
  • toRef (477-491)
  • isRef (46-48)
  • Ref (28-37)
packages/reactivity/src/baseHandlers.ts (1)
  • get (55-134)
packages/reactivity/src/baseHandlers.ts (2)
packages/shared/src/general.ts (3)
  • isArray (39-39)
  • isIntegerKey (77-81)
  • hasOwn (34-37)
packages/reactivity/src/reactive.ts (3)
  • isReadonly (336-338)
  • isShallow (340-342)
  • toRaw (378-381)
packages/reactivity/src/ref.ts (4)
packages/reactivity/src/reactive.ts (4)
  • toRaw (378-381)
  • isProxy (351-353)
  • isShallow (340-342)
  • Target (18-24)
packages/reactivity/src/index.ts (6)
  • toRaw (33-33)
  • isProxy (29-29)
  • isShallow (28-28)
  • ReactiveFlags (82-82)
  • unref (8-8)
  • isRef (4-4)
packages/shared/src/general.ts (1)
  • isIntegerKey (77-81)
packages/reactivity/src/dep.ts (2)
  • Dep (67-205)
  • getDepFromReactive (391-397)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: test / e2e-test
🔇 Additional comments (4)
packages/reactivity/src/baseHandlers.ts (1)

142-177: Array index writes now consistently treat elements as raw values

Guarding the ref-unwrapping branch with !isArrayWithIntegerKey and reusing isArrayWithIntegerKey for hadKey makes array integer index writes behave like plain value replacement (not “mutate nested ref in-place”), matching the existing get-trap behavior that does not unwrap refs for numeric array indices. This keeps object properties with refs using the nested-ref update path while letting array indices be managed explicitly via their refs or via toRef’s new ObjectRefImpl logic. Looks correct and aligns array semantics with the broader collection design.

packages/reactivity/__tests__/ref.spec.ts (1)

312-337: New toRef tests comprehensively cover non-reactive, shallow, readonly, and array edge cases

The added suites around toRef (plain objects with nested refs + defaults, array indices/custom props, lazy computed evaluation through proxies, shallowReactive/shallowReadonly combos, and readonly-wrapped shallow/regular proxies) tightly exercise the new ObjectRefImpl logic and the array handler tweaks. The scenarios match the PR’s listed edge cases and should catch regressions in ref unwrapping and proxy-bypass behavior.

Also applies to: 339-389, 414-555

packages/reactivity/src/ref.ts (2)

352-402: ObjectRefImpl’s _shallow detection and setter routing match the proxy/unwrapping model

The new _raw + _shallow logic in ObjectRefImpl cleanly separates when the object-ref itself should handle unwrapping/nested-ref writes vs. when to defer entirely to the underlying proxies:

  • Walking the proxy chain via RAW and using !isProxy(obj) || isShallow(obj) means any non‑shallow proxy (e.g. reactive, readonly) flips _shallow to false, so get value just returns what the proxy gives and set value goes through the proxy, preserving readonly semantics.
  • Arrays with integer keys are explicitly exempted from that walk, keeping _shallow true so get/set can compensate for the array handlers’ “do not unwrap element refs” rule and provide the expected toRef(array, index) behavior.
  • The setter’s isRef(this._raw[this._key]) guard ensures nested-ref forwarding only happens while the raw slot actually holds a ref; once the slot is replaced (e.g. set to undefined), subsequent writes correctly fall back to normal proxy set.
  • The dep getter delegation to getDepFromReactive(this._raw, this._key) nicely reuses the existing dependency graph so triggerRef works with property refs without duplicating tracking.

Taken together, this lines up with the new tests for shallow/reactive/readonly combinations and the array index cases.


493-499: propertyToRef no longer returns the existing nested ref instance

propertyToRef now always returns a fresh ObjectRefImpl instead of returning source[key] when it happens to be a ref. This is an intentional behavioral change:

  • It fixes the lazy-evaluation issue for computed props on reactive objects, because toRef no longer has to read the property at creation time.
  • It makes toRef semantics uniform across plain/reactive/shallow/readonly sources and arrays, at the cost of no longer preserving object identity (toRef(obj, 'x') !== obj.x even when obj.x is already a ref).

Given the updated tests no longer rely on identity and explicitly cover the new behavior, this change looks deliberate and consistent; just worth keeping in mind as a subtle observable change for library consumers that previously depended on === checks.

@vuejs vuejs deleted a comment from edison1105 Nov 24, 2025
@vue-bot
Copy link
Contributor

vue-bot commented Nov 24, 2025

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools success success
primevue success success
radix-vue success success
nuxt failure failure
quasar success success
pinia success success
test-utils success success
router failure failure
vite-plugin-vue failure success
vue-macros failure failure
vuetify success success
vitepress success success
vueuse success success
vue-simple-compiler success success
vue-i18n failure failure
vant success success

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@edison1105 edison1105 merged commit 0d2357e into vuejs:main Nov 24, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. ready to merge The PR is ready to be merged. scope: reactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants